/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.databox.monitorproperty;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Point;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Vector;

import javax.swing.JSplitPane;

import demo.DemoPane;

import twaver.AlarmSeverity;
import twaver.Element;
import twaver.Link;
import twaver.TDataBox;
import twaver.TWaverConst;
import twaver.TaskAdapter;
import twaver.TaskScheduler;
import twaver.chart.BarChart;
import twaver.chart.Item;
import twaver.chart.LineChart;
import twaver.chart.LineItem;
import twaver.Node;
import twaver.network.TNetwork;
import twaver.TWaverUtil;

public class MonitorPropertyDemo extends DemoPane {

    LineChart lineChart;
    BarChart barChart;

    TDataBox box = new TDataBox();
    TNetwork network = new TNetwork(box);

    public MonitorPropertyDemo() {
        network.setTextureBackground("/demo/resource/images/texture.png");
        network.setToolbarByName(TWaverConst.DEFAULT_TOOLBAR);
        // init box
        final Node start = new Node("S");
        start.setName("Node A");
        start.setToolTipText("Link start from here.");
        start.putLabelColor(Color.GREEN);
        start.putBorderColor(Color.WHITE);
        start.getAlarmState().setNewAlarmCount(AlarmSeverity.CRITICAL, 2);
        box.addElement(start);

        final Node end = new Node("E");
        end.setName("Node B");
        end.setToolTipText("Link end to here.");
        end.putLabelColor(Color.GREEN);
        end.putBorderColor(Color.WHITE);
        end.getAlarmState().setNewAlarmCount(AlarmSeverity.WARNING, 3);
        box.addElement(end);

        final Link link = new Link("L", start, end);
        link.setLinkType(TWaverConst.LINK_TYPE_FLEXIONAL);
        link.putLink3D(true);
        link.putLinkWidth(6);
        link.putBorderColor(Color.WHITE);
        box.addElement(link);
        
        // init chart
        Vector items = new Vector();
        final LineItem red = new LineItem("red", 0, Color.RED);
        final LineItem green = new LineItem("green", 0, Color.GREEN);
        final LineItem blue = new LineItem("blue", 0, Color.BLUE);
        items.addElement(red);
        items.addElement(green);
        items.addElement(blue);
        lineChart = new LineChart(items, "Link Color Value", Color.BLACK, Color.WHITE);

        items = new Vector();
        final Item startX = new Item("startX", 0, Color.RED);
        final Item startY = new Item("startY", 0, Color.BLUE);
        final Item endX = new Item("endX", 0, Color.RED);
        final Item endY = new Item("endY", 0, Color.BLUE);
        items.addElement(startX);
        items.addElement(startY);
        items.addElement(endX);
        items.addElement(endY);
        barChart = new BarChart(items, "Coordinate", Color.WHITE);

        JSplitPane chartSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, barChart, lineChart);
        JSplitPane mainSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT, network, chartSplit);
        mainSplit.setDividerLocation(200);
        mainSplit.setContinuousLayout(true);
        this.add(mainSplit, BorderLayout.CENTER);

        // listen node's location property
        box.addElementPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals(TWaverConst.PROPERTYNAME_LOCATION)) {
                    Element element = (Element) evt.getSource();
                    if (element == start) {
                        Point loaction = element.getLocation();
                        startX.setValue(loaction.x);
                        startY.setValue(loaction.y);
                        startX.setToolTipText("startX=" + loaction.x);
                        startY.setToolTipText("startY=" + loaction.y);
                    }
                    if (element == end) {
                        Point loaction = element.getLocation();
                        endX.setValue(loaction.x);
                        endY.setValue(loaction.y);
                        endX.setToolTipText("endX=" + loaction.x);
                        endY.setToolTipText("endY=" + loaction.y);
                    }
                }
            }
        });
        
        // listen link client property
        link.addPropertyChangeListener(
            TWaverConst.CLIENT_PROPERTY_PREFIX + TWaverConst.PROPERTYNAME_LINK_COLOR,
            new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                Element element = (Element) evt.getSource();
                if (element == link) {
                    Color color = (Color) evt.getNewValue();
                    red.addValue(color.getRed());
                    green.addValue(color.getGreen());
                    blue.addValue(color.getBlue());
                    if (lineChart.valueCount() > 30) {
                        lineChart.removeHead(1);
                    }
                }
            }
        });

        // init value
        start.setLocation(100, 100);
        end.setLocation(400, 50);
        link.putLinkColor(Color.MAGENTA);
        link.putLinkColor(Color.CYAN);

        // init task
        TaskScheduler.getInstance().register(new TaskAdapter() {
            public void run(long clock) {
            	if(!network.isShowing()){
            		return;
            	}
                int r = TWaverUtil.getRandomInt(255);
                int g = TWaverUtil.getRandomInt(255);
                int b = TWaverUtil.getRandomInt(255);
                link.putLinkColor(new Color(r, g, b));
            }

            public int getInterval() {
                return 1000;
            }
        });

    }

}